import 'package:appflutterhomeschool/model/questions_model.dart';
import 'package:appflutterhomeschool/provider/provider_widget.dart';
import 'package:appflutterhomeschool/res/styles.dart';
import 'package:appflutterhomeschool/ui/widgets/button_widget.dart';
import 'package:appflutterhomeschool/ui/widgets/my_app_bar.dart';
import 'package:appflutterhomeschool/ui/widgets/notice/form_widget.dart';
import 'package:appflutterhomeschool/ui/widgets/widgets.dart';
import 'package:appflutterhomeschool/view_model/notice/new_form_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///用户： ninetailedfox
///时间： 2020/7/19
///邮箱： 1037438704@qq.com
///功能： 创建表单
class NewFormPage extends StatefulWidget {
  @override
  _NewFormPageState createState() => _NewFormPageState();
}

class _NewFormPageState extends State<NewFormPage> {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<NewFormViewModel>(
      model: NewFormViewModel(context),
      onModelReady: (value) => value.initView(),
      builder: (context, viewModel, child) {
        return GestureDetector(
          onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
          child: Scaffold(
            backgroundColor: Colors.white,
            appBar: MyAppBar2(title: "新建表单"),
            body: Stack(
              children: <Widget>[
                Positioned(
                  left: 0,
                  right: 0,
                  top: 0,
                  bottom: ScreenUtil().setWidth(115),
                  child: SingleChildScrollView(
                    child: Column(
                      children: <Widget>[
                        AddresseeWidget(
                          addressee: viewModel.student,
                          onTap: () => viewModel.initEvent(0),
                        ),
                        ReplyTitleWidget(
                            textEditingController: viewModel.titleText),
                        ReplyInputBox(
                            textEditingController: viewModel.textContent),
                        ListView.builder(
                            shrinkWrap: true,
                            physics: NeverScrollableScrollPhysics(),
                            itemCount: viewModel.questionsModelList.length,
                            itemBuilder: (context, index) {
                              QuestionsModel questionModel =
                                  viewModel.questionsModelList[index];
                              return Column(
                                children: <Widget>[
                                  TitleMenuWidget(
                                    titleContext: questionModel.body,
                                    onSelected: (value) {
                                      if (value == "编辑") {
                                        viewModel.editOnTap(index);
                                      } else {
                                        viewModel.deleteOnTap(index);
                                      }
                                    },
                                  ),
                                  itemGetWidget(questionModel)
                                ],
                              );
                            }),
                        //选择题目
                        Container(
                          margin: EdgeInsets.fromLTRB(
                              ScreenUtil().setWidth(35),
                              ScreenUtil().setWidth(50),
                              ScreenUtil().setWidth(35),
                              ScreenUtil().setWidth(200)),
                          child: ButtonWidget().outlineButtonWidget(
                              textStyle: TextStyles.textBlue37,
                              buttonName: "添加题目",
                              borderSideColor: Colors.blue,
                              widthDouble: double.maxFinite,
                              heightDouble: ScreenUtil().setWidth(115),
                              onPressed: viewModel.addItem),
                        ),
                      ],
                    ),
                  ),
                ),
                Positioned(
                  bottom: 0,
                  left: 0,
                  right: 0,
                  child: ButtonWidget().rectangleButtonWidget(
                      buttonName: "发布", onPressed: viewModel.releaseOnTap),
                )
              ],
            ),
          ),
        );
      },
    );
  }

  // ignore: missing_return
  Widget itemGetWidget(QuestionsModel questionModel) {
    List<Options> optionsList = questionModel.options;
    if (questionModel.questionType == "SC") {
      return SingleChoiceWidget(optionsList: optionsList);
    } else if (questionModel.questionType == "MC") {
      return MultipleChoiceWidget(optionsList: optionsList);
    } else if (questionModel.questionType == "C") {
      return FormTextWidget();
    }
  }
}
